package com.atguigu.crowd.funding.service.impl;

import com.atguigu.crowd.funding.entity.Admin;
import com.atguigu.crowd.funding.entity.AdminExample;
import com.atguigu.crowd.funding.mapper.AdminMapper;
import com.atguigu.crowd.funding.service.api.AdminService;
import com.atguigu.crowd.funding.util.CrowdFundingConstant;
import com.atguigu.crowd.funding.util.CrowdFundingUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author hftang
 * @date 2019-10-23 10:07
 * @desc
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PasswordEncoder bCryptPasswordEncoder;


    public List<Admin> getAll() {
        return this.adminMapper.selectByExample(new AdminExample());
    }

    public Admin doLogin(String loginAcct, String userPswd) {


        AdminExample example = new AdminExample();
        AdminExample.Criteria exampleCriteria = example.createCriteria();
        exampleCriteria.andLoginAcctEqualTo(loginAcct);
        List<Admin> adminList = this.adminMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(adminList)) {
            return null;
        }

        Admin admin = adminList.get(0);
        String dataPwd = admin.getUserPswd();
        String md5Pwd = CrowdFundingUtils.md5(userPswd);

        if (StringUtils.equals(md5Pwd, dataPwd)) {

            return admin;
        }

        return null;


    }

    /**
     * 分页查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */

    @Override
    public PageInfo<Admin> queryForKeywordSearch(String keyword, Integer pageNum, Integer pageSize) {

        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        List<Admin> admins = this.adminMapper.selectAdminListByKeyword(keyword);


        return new PageInfo<Admin>(admins);
    }

    @Override
    public void batchRemove(List<Integer> ids) {


        AdminExample example = new AdminExample();
        example.createCriteria().andIdIn(ids);
        this.adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserPswd();
        String encode = bCryptPasswordEncoder.encode(userPswd);
        admin.setUserPswd(encode);

        this.adminMapper.insert(admin);
    }

    @Override
    public Admin findAdminById(Integer adminId) {

        Admin admin = this.adminMapper.selectByPrimaryKey(adminId);

        return admin;
    }

    @Override
    public void updateAdmin(Admin admin) {
        String encode = this.bCryptPasswordEncoder.encode(admin.getUserPswd());
        admin.setUserPswd(encode);
       this.adminMapper.updateByPrimaryKeySelective(admin);
    }
}
